import logging
from logging.handlers import RotatingFileHandler

def singleton(cls):
    instances = {}
    def wrapper(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]
    return wrapper


@singleton
class CustomLogger:
    def __init__(self, log_file='./log/gitee_review.log', max_file_size=200*1024, backup_count=5):
        """
        初始化日志类
        :param log_file: 日志文件名
        :param max_file_size: 单个文件的最大大小 (字节), 默认200KB
        :param backup_count: 最大备份日志文件数量
        """
        # 创建日志记录器
        self.logger = logging.getLogger('CustomLogger')
        self.logger.setLevel(logging.DEBUG)

        # 创建一个日志格式器
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

        # 创建控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.DEBUG)
        console_handler.setFormatter(formatter)

        # 创建文件处理器，支持文件大小限制和日志轮转
        file_handler = RotatingFileHandler(log_file, maxBytes=max_file_size, backupCount=backup_count)
        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(formatter)

        # 添加处理器到日志器
        self.logger.addHandler(console_handler)
        self.logger.addHandler(file_handler)

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def warning(self, message):
        self.logger.warning(message)

    def error(self, message):
        self.logger.error(message)

    def log_critical(self, message):
        self.logger.critical(message)


if __name__ == '__main__':
    logger = CustomLogger()
    while (True):
        logger.info("hello world")
